/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: Anonymous
 * License Type: Purchased
 */
package com.estate.datalogic.impl;

import org.orm.*;
import org.hibernate.Query;
import java.util.List;
import com.estate.datalogic.*;

/**
 * ORM-Persistable Class
 */
public class AdvertisementDAOImpl implements com.estate.datalogic.dao.AdvertisementDAO {
	private static final org.apache.log4j.Logger _logger = org.apache.log4j.Logger.getLogger(AdvertisementDAOImpl.class);
	public Advertisement loadAdvertisementByORMID(int idAssignment) throws PersistentException  {
		PersistentSession session = com.estate.datalogic.EstatePersistentManager.instance().getSession();
		return loadAdvertisementByORMID(session, idAssignment);
	}
	
	public Advertisement loadAdvertisementByORMID(PersistentSession session, int idAssignment) throws PersistentException {
		return (Advertisement) session.load(com.estate.datalogic.Advertisement.class, new Integer(idAssignment));
	}
	
	public Advertisement[] listAdvertisementByQuery(String condition, String orderBy) throws PersistentException  {
		PersistentSession session = com.estate.datalogic.EstatePersistentManager.instance().getSession();
		return listAdvertisementByQuery(session, condition, orderBy);
	}
	
	public Advertisement[] listAdvertisementByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From com.estate.datalogic.Advertisement as Advertisement");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		Query query = session.createQuery(sb.toString());
		try {
			List list = query.list();
			return (Advertisement[]) list.toArray(new Advertisement[list.size()]);
		}
		catch (Exception e) {
			_logger.error("listAdvertisementByQuery(PersistentSession session, String condition, String orderBy)", e);
			throw new PersistentException(e);
		}
	}
	
	public Advertisement loadAdvertisementByQuery(String condition, String orderBy) throws PersistentException {
		PersistentSession session = com.estate.datalogic.EstatePersistentManager.instance().getSession();
		return loadAdvertisementByQuery(session, condition, orderBy);
	}
	
	public Advertisement loadAdvertisementByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		Advertisement[] advertisements = listAdvertisementByQuery(session, condition, orderBy);
		if (advertisements != null && advertisements.length > 0)
			return advertisements[0];
		else
			return null;
	}
	
	public static java.util.Iterator iterateAdvertisementByQuery(String condition, String orderBy) throws PersistentException  {
		PersistentSession session = com.estate.datalogic.EstatePersistentManager.instance().getSession();
		return iterateAdvertisementByQuery(session, condition, orderBy);
	}
	
	public static java.util.Iterator iterateAdvertisementByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From com.estate.datalogic.Advertisement as Advertisement");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		Query query = session.createQuery(sb.toString());
		try {
			return query.iterate();
		}
		catch (Exception e) {
			_logger.error("iterateAdvertisementByQuery(PersistentSession session, String condition, String orderBy)", e);
			throw new PersistentException(e);
		}
	}
	
	public Advertisement createAdvertisement() {
		return new com.estate.datalogic.Advertisement();
	}
	
	public boolean save(com.estate.datalogic.Advertisement advertisement)throws PersistentException {
		try {
			com.estate.datalogic.EstatePersistentManager.instance().saveObject(advertisement);
			return true;
		}
		catch (Exception e) {
			_logger.error("save(com.estate.datalogic.Advertisement advertisement)", e);
			throw new PersistentException(e);
		}
	}
	
	public boolean delete(com.estate.datalogic.Advertisement advertisement)throws PersistentException {
		try {
			com.estate.datalogic.EstatePersistentManager.instance().deleteObject(advertisement);
			return true;
		}
		catch (Exception e) {
			_logger.error("delete(com.estate.datalogic.Advertisement advertisement)", e);
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(com.estate.datalogic.Advertisement advertisement)throws PersistentException {
		try {
			if(advertisement.getClient() != null) {
				advertisement.getClient().advertisement.remove(advertisement);
			}
			
			if(advertisement.getAd_typeidAd_type() != null) {
				advertisement.getAd_typeidAd_type().advertisement.remove(advertisement);
			}
			
			if(advertisement.getTown() != null) {
				advertisement.getTown().setAdvertisement(null);
			}
			
			return delete(advertisement);
		}
		catch(Exception e) {
			_logger.error("deleteAndDissociate()", e);
			throw new PersistentException(e);
		}
	}
	
	public boolean refresh(com.estate.datalogic.Advertisement advertisement)throws PersistentException {
		try {
			com.estate.datalogic.EstatePersistentManager.instance().getSession().refresh(advertisement);
			return true;
		}
		catch (Exception e) {
			_logger.error("refresh(com.estate.datalogic.Advertisement advertisement)", e);
			throw new PersistentException(e);
		}
	}
	
}
